/**
 * Name: Paul Rosa
 * 
 * Date: January 12 2012
 */
import java.util.*;

public class ArrayPractice {

	/**
	 * Constructs an array of integers.
	 */
	public ArrayPractice(int[] a) {
		this.a = a;
	}

	/**
	 * Precondition: 0 <= from < a.length Postcondition: Finds the smallest
	 * element in a tail range of the array.
	 * 
	 * @param from:
	 *            the first position in 'a' to compare
	 * @return the position of the smallest element in the range a[from] . . .
	 *         a[a.length - 1]
	 */
	public int minimumPosition(int from) {
		int minI = from;
		for (int i = from; i < a.length; i++)
			if (a[minI] >= a[i])
				minI = i;
		return minI;
	}

	/**
	 * Precondition: 0 <= i < a.length, 0 <= j < a.length Postcondition: Swaps
	 * two entries of the array.
	 * 
	 * @param i:
	 *            the first position to swap
	 * @param j:
	 *            the second position to swap
	 */
	public void swap(int i, int j) {
		int oi = a[i];
		int oj = a[j];
		a[i] = oj;
		a[j] = oi;
	}

	/**
	 * Precondition: 0 <= start <= end < a.length Postcondition: Prints the
	 * values of the array from start to end inclusive.
	 * 
	 * @param start:
	 *            the first position to print
	 * @param end:
	 *            the last position to print.
	 */
	public String printArray(int start, int end) {
		return ArrayUtil.printArray(a, start, end);
	}

	public int findIndexToInsert(int insertNum) {
		for (int i = 0; i < a.length; i++)
			if (!(a[i] < insertNum))
				return i;
		return a.length;
	}

	public void insetSorted(int insertSorted) {
		int where = findIndexToInsert(insertSorted);
		int[] t = new int[a.length + 1];
		for (int i = 0; i < t.length; i++) {

			if (i == where)
				t[i] = insertSorted;
			else if (i > where)
				t[i] = a[i - 1];
			else
				t[i] = a[i];
		}
		a = t;
	}

	public int[] a;
}